//
//  RAPEmoticonKeyboard.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/7/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

let RAPEmoticonKeyboardMenuBarHeight: CGFloat = 30
let RAPEmoticonKeyboardPredefinedHeight: CGFloat = 216

class RAPEmoticonKeyboard: UIView {
    // MARK: - 自定义属性
    
    // MARK: - 系统方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        getReady()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func getReady() {
        backgroundColor = UIColor(white: 241 / 255, alpha: 1)
        
        // 添加子控件
        addSubview(menuBar)
        addSubview(presentationView)
        addSubview(pageControl)
        
        installConstraints()
        
        /*
        主队列异步，使得主队列在执行当前任务之后，在执行这个任务（队列最末尾）
        */
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.presentationView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 1), atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
        }
    }
    
    private func installConstraints() {
        menuBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(RAPEmoticonKeyboardMenuBarHeight)
        }
        
        presentationView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.height.equalTo(RAPEmoticonKeyboardPredefinedHeight - RAPEmoticonKeyboardMenuBarHeight)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(menuBar.snp_top)
        }
    }
    
    ///  设置page control
    ///
    ///  - parameter indexPath: 当前的indexpath
    private func setPageControl(indexPath: NSIndexPath) {
        pageControl.numberOfPages = RAPEmoticonKeyboardViewModel.sharedViewModel.tripleDArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
    
    // MARK: - 懒加载控件
    private lazy var menuBar: RAPEmoticonKeyboardMenuBar = {
        let menuBar = RAPEmoticonKeyboardMenuBar(frame: CGRectZero)
        /*
         设置回调，点击菜单按钮，表情栏相应滚动
        */
        menuBar.menuButtonClickedClosure = {
            [unowned self]
            (buttonType: RAPEmoticonType) -> () in
            self.presentationView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: buttonType.rawValue - 1000), atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
            self.setPageControl(NSIndexPath(forItem: 0, inSection: buttonType.rawValue - 1000))
        }
        
        return menuBar
    }()
    
    private lazy var presentationView: RAPEmoticonPresentationView = {
        let presentationView = RAPEmoticonPresentationView()
        
        /*
        presentation view进行滚动的回调
        */
        presentationView.onScrollingClosure = {
            [unowned self]
            (cellIndexPath: NSIndexPath) -> () in

            self.menuBar.selectButtonBySection(cellIndexPath.section )
            
            self.setPageControl(cellIndexPath)
        }
        
        return presentationView
    }()
    
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.hidesForSinglePage = true
        pageControl.userInteractionEnabled = false
        
        /*
        利用KVC设置隐藏属性（图片），因为只有设置颜色的公共api
        */
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        
        return pageControl
    }()
}
